﻿namespace ElencySolutions.ImageMap.Properties
{
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using EPiServer.Core.PropertySettings;

    public class HotSpotPropertySettingsUI : PropertySettingsControlBase
    {
        private TextBox _imageUrl;
        private TextBox _imageHeight;
        private TextBox _imageWidth;
        private TextBox _popupHeight;
        private TextBox _popupWidth;


        /// <summary>
        /// Adds the new row.
        /// </summary>
        /// <param name="label">The label.</param>
        /// <param name="control">The control.</param>
        protected void AddNewRow(Label label, Control control)
        {
            AddNewRow(label, control, false, null);
        }

        /// <summary>
        /// Adds the new row.
        /// </summary>
        /// <param name="label">The label.</param>
        /// <param name="control">The control.</param>
        /// <param name="scroll">if set to <c>true</c> [scroll].</param>
        protected void AddNewRow(Label label, Control control, bool scroll, List<Control> validationControls)
        {
            Literal literal = new Literal { Text = "<div style=\"clear:both;margin-bottom:5px;padding-bottom:5px;float:left;width:100%\">" };
            Controls.Add(literal);
            literal = new Literal { Text = "<div style=\"width:210px;float:left\">" };
            Controls.Add(literal);
            label.Attributes["style"] = "width:210px!important";
            Controls.Add(label);
            literal = new Literal { Text = "</div>" };
            Controls.Add(literal);
            literal = new Literal { Text = "<div style=\"margin-left:15px;float:left;width:420px;float:left\">" };
            Controls.Add(literal);

            if (scroll)
            {
                literal = new Literal { Text = "<div style=\"width:100%;overflow:auto;padding:3px;border:solid 1px #B4B4B4\">" };
                Controls.Add(literal);
            }

            Controls.Add(control);

            if (validationControls != null)
            {
                foreach (Control validationControl in validationControls)
                    Controls.Add(validationControl);
            }

            if (scroll)
            {
                literal = new Literal { Text = "</div>" };
                Controls.Add(literal);
            }

            literal = new Literal { Text = "</div>" };
            Controls.Add(literal);
            literal = new Literal { Text = "</div>" };
            Controls.Add(literal);
        }

        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to 
        /// create any child controls they contain in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            Literal literal = new Literal { Text = "<div class=\"epi-size10\">" };
            Controls.Add(literal);

            literal = new Literal { Text = string.Format("<fieldset><legend>{0}</legend><div class=\"epi-size10\">", LanguageHelper.Translate("/elencySolutionsImageMap/hotSpotPropertySettings")) };
            Controls.Add(literal);

            // image url property name
            string id = "ImageUrl";
            _imageUrl = new TextBox { ID = id };

            Label label = new Label { Text = LanguageHelper.Translate("/elencySolutionsImageMap/imageUrlPropertyName"), AssociatedControlID = id };
            AddNewRow(label, _imageUrl);

            // image height property name
            id = "ImageHeight";
            _imageHeight = new TextBox { ID = id };

            label = new Label { Text = LanguageHelper.Translate("/elencySolutionsImageMap/imageHeightPropertyName"), AssociatedControlID = id };
            AddNewRow(label, _imageHeight);

            // image height property name
            id = "ImageWidth";
            _imageWidth = new TextBox { ID = id };

            label = new Label { Text = LanguageHelper.Translate("/elencySolutionsImageMap/imageWidthPropertyName"), AssociatedControlID = id };
            AddNewRow(label, _imageWidth);

            // popup height
            id = "PopupHeight";
            _popupHeight = new TextBox { ID = id };

            label = new Label { Text = LanguageHelper.Translate("/elencySolutionsImageMap/popupHeight"), AssociatedControlID = id };

            RequiredFieldValidator rfv = new RequiredFieldValidator
            {
                ControlToValidate = id,
                ErrorMessage = LanguageHelper.Translate("/elencySolutionsImageMap/popupHeightRequired"),
                Text = "*",
                Display = ValidatorDisplay.Dynamic
            };

            RegularExpressionValidator rev = new RegularExpressionValidator
            {
                ControlToValidate = id,
                ErrorMessage = LanguageHelper.Translate("/elencySolutionsImageMap/popupHeightValidation"),
                Text = "*",
                Display = ValidatorDisplay.Dynamic,
                ValidationExpression = "^[0-9]*$"
            };
            AddNewRow(label, _popupHeight, false, new List<Control> { rfv, rev });

            // popup width
            id = "PopupWidth";
            _popupWidth = new TextBox { ID = id };

            label = new Label { Text = LanguageHelper.Translate("/elencySolutionsImageMap/popupWidth"), AssociatedControlID = id };

            rfv = new RequiredFieldValidator
            {
                ControlToValidate = id,
                ErrorMessage = LanguageHelper.Translate("/elencySolutionsImageMap/popupWidthRequired"),
                Text = "*",
                Display = ValidatorDisplay.Dynamic
            };

            rev = new RegularExpressionValidator
            {
                ControlToValidate = id,
                ErrorMessage = LanguageHelper.Translate("/elencySolutionsImageMap/popupWidthValidation"),
                Text = "*",
                Display = ValidatorDisplay.Dynamic,
                ValidationExpression = "^[0-9]*$"
            };
            AddNewRow(label, _popupWidth, false, new List<Control> { rfv, rev });

            literal = new Literal { Text = "</div></fieldset>" };
            Controls.Add(literal);

            literal = new Literal { Text = "</div>" };
            Controls.Add(literal);
        }

        /// <summary>
        /// Populates control values
        /// </summary>
        /// <param name="propertySettings">The property settings.</param>
        public override void LoadSettingsUI(IPropertySettings propertySettings)
        {
            EnsureChildControls();

            HotSpotPropertySettings settings = propertySettings as HotSpotPropertySettings;

            if (settings == null)
                return;

            _imageUrl.Text = string.IsNullOrEmpty(settings.ImageUrlPropertyName) ? string.Empty : settings.ImageUrlPropertyName.Trim();
            _imageHeight.Text = string.IsNullOrEmpty(settings.ImageHeightPropertyName) ? string.Empty : settings.ImageHeightPropertyName.Trim();
            _imageWidth.Text = string.IsNullOrEmpty(settings.ImageWidthPropertyName) ? string.Empty : settings.ImageWidthPropertyName.Trim();
            _popupHeight.Text = settings.PopupHeight.HasValue ? settings.PopupHeight.Value.ToString() : HotSpotPropertyControl.DEFAULT_POPUP_HEIGHT.ToString();
            _popupWidth.Text = settings.PopupWidth.HasValue ? settings.PopupWidth.Value.ToString() : HotSpotPropertyControl.DEFAULT_POPUP_WIDTH.ToString();
        }

        /// <summary>
        /// Updates the settings.
        /// </summary>
        /// <param name="propertySettings">The property settings.</param>
        public override void UpdateSettings(IPropertySettings propertySettings)
        {
            EnsureChildControls();

            if (!Page.IsValid)
                return;

            HotSpotPropertySettings settings = propertySettings as HotSpotPropertySettings;
            settings.ImageUrlPropertyName = _imageUrl.Text.Trim();
            settings.ImageHeightPropertyName = _imageHeight.Text.Trim();
            settings.ImageWidthPropertyName = _imageWidth.Text.Trim();
            settings.PopupHeight = int.Parse(_popupHeight.Text);
            settings.PopupWidth = int.Parse(_popupWidth.Text);
        }

    }
}
